//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.activiti.engine.impl.persistence.entity.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.persistence.AbstractManager;
import org.activiti.engine.impl.persistence.CachedEntityMatcher;
import org.activiti.engine.impl.persistence.SingleCachedEntityMatcher;
import org.activiti.engine.impl.persistence.cache.CachedEntity;
import org.activiti.engine.impl.persistence.cache.EntityCache;
import org.activiti.engine.impl.persistence.entity.Entity;

public abstract class AbstractDataManager<EntityImpl extends Entity> extends AbstractManager implements DataManager<EntityImpl> {
  public AbstractDataManager(ProcessEngineConfigurationImpl processEngineConfiguration) {
    super(processEngineConfiguration);
  }

  public abstract Class<? extends EntityImpl> getManagedEntityClass();

  public List<Class<? extends EntityImpl>> getManagedEntitySubClasses() {
    return null;
  }

  protected DbSqlSession getDbSqlSession() {
    
    return (DbSqlSession)this.getSession(DbSqlSession.class);
  }

  protected EntityCache getEntityCache() {
    return (EntityCache)this.getSession(EntityCache.class);
  }

  public EntityImpl findById(String entityId) {
    if (entityId == null) {
      return null;
    } else {
      EntityImpl cachedEntity = (EntityImpl) this.getEntityCache().findInCache(this.getManagedEntityClass(), entityId);
      return cachedEntity != null ? cachedEntity : this.getDbSqlSession().selectById(this.getManagedEntityClass(), entityId, false);
    }
  }

  public void insert(EntityImpl entity) {
    this.getDbSqlSession().insert(entity);
  }

  public EntityImpl update(EntityImpl entity) {
    this.getDbSqlSession().update(entity);
    return entity;
  }

  public void delete(String id) {
    EntityImpl entity = this.findById(id);
    this.delete(entity);
  }

  public void delete(EntityImpl entity) {
    this.getDbSqlSession().delete(entity);
  }

  protected EntityImpl getEntity(String selectQuery, Object parameter, SingleCachedEntityMatcher<EntityImpl> cachedEntityMatcher, boolean checkDatabase) {
    Iterator var5 = this.getEntityCache().findInCache(this.getManagedEntityClass()).iterator();

    Entity cachedEntity;
    do {
      if (!var5.hasNext()) {
        if (checkDatabase) {
          return (EntityImpl) this.getDbSqlSession().selectOne(selectQuery, parameter);
        }

        return null;
      }

      cachedEntity = (Entity)var5.next();
    } while(!cachedEntityMatcher.isRetained((EntityImpl) cachedEntity, parameter));

    return (EntityImpl) cachedEntity;
  }

  protected List<EntityImpl> getList(String dbQueryName, Object parameter, CachedEntityMatcher<EntityImpl> cachedEntityMatcher, boolean checkCache) {
    Collection<EntityImpl> result = this.getDbSqlSession().selectList(dbQueryName, parameter);
    if (checkCache) {
      Collection<CachedEntity> cachedObjects = this.getEntityCache().findInCacheAsCachedObjects(this.getManagedEntityClass());
      if (cachedObjects != null && cachedObjects.size() > 0 || this.getManagedEntitySubClasses() != null) {
        HashMap<String, EntityImpl> entityMap = new HashMap(((Collection)result).size());
        Iterator var8 = ((Collection)result).iterator();

        while(var8.hasNext()) {
          EntityImpl entity = (EntityImpl) var8.next();
          entityMap.put(entity.getId(), entity);
        }

        if (cachedObjects != null && cachedEntityMatcher != null) {
          var8 = cachedObjects.iterator();

          while(var8.hasNext()) {
            CachedEntity cachedObject = (CachedEntity)var8.next();
            EntityImpl cachedEntity = (EntityImpl) cachedObject.getEntity();
            if (cachedEntityMatcher.isRetained((Collection)result, cachedObjects, cachedEntity, parameter)) {
              entityMap.put(cachedEntity.getId(), cachedEntity);
            }
          }
        }

        if (this.getManagedEntitySubClasses() != null && cachedEntityMatcher != null) {
          var8 = this.getManagedEntitySubClasses().iterator();

          label62:
          while(true) {
            Collection subclassCachedObjects;
            do {
              if (!var8.hasNext()) {
                break label62;
              }

              Class<? extends EntityImpl> entitySubClass = (Class)var8.next();
              subclassCachedObjects = this.getEntityCache().findInCacheAsCachedObjects(entitySubClass);
            } while(subclassCachedObjects == null);

            Iterator var11 = subclassCachedObjects.iterator();

            while(var11.hasNext()) {
              CachedEntity subclassCachedObject = (CachedEntity)var11.next();
              EntityImpl cachedSubclassEntity = (EntityImpl) subclassCachedObject.getEntity();
              if (cachedEntityMatcher.isRetained((Collection)result, cachedObjects, cachedSubclassEntity, parameter)) {
                entityMap.put(cachedSubclassEntity.getId(), cachedSubclassEntity);
              }
            }
          }
        }

        result = entityMap.values();
      }
    }

    if (((Collection)result).size() > 0) {
      Iterator<EntityImpl> resultIterator = ((Collection)result).iterator();

      while(resultIterator.hasNext()) {
        if (this.getDbSqlSession().isEntityToBeDeleted((Entity)resultIterator.next())) {
          resultIterator.remove();
        }
      }
    }

    return new ArrayList((Collection)result);
  }

  protected List<EntityImpl> getListFromCache(CachedEntityMatcher<EntityImpl> entityMatcher, Object parameter) {
    Collection<CachedEntity> cachedObjects = this.getEntityCache().findInCacheAsCachedObjects(this.getManagedEntityClass());
    DbSqlSession dbSqlSession = this.getDbSqlSession();
    List<EntityImpl> result = new ArrayList(cachedObjects.size());
    Iterator var6;
    if (cachedObjects != null && entityMatcher != null) {
      var6 = cachedObjects.iterator();

      while(var6.hasNext()) {
        CachedEntity cachedObject = (CachedEntity)var6.next();
        EntityImpl cachedEntity = (EntityImpl) cachedObject.getEntity();
        if (entityMatcher.isRetained((Collection)null, cachedObjects, cachedEntity, parameter) && !dbSqlSession.isEntityToBeDeleted(cachedEntity)) {
          result.add(cachedEntity);
        }
      }
    }

    if (this.getManagedEntitySubClasses() != null && entityMatcher != null) {
      var6 = this.getManagedEntitySubClasses().iterator();

      while(true) {
        Collection subclassCachedObjects;
        do {
          if (!var6.hasNext()) {
            return result;
          }

          Class<? extends EntityImpl> entitySubClass = (Class)var6.next();
          subclassCachedObjects = this.getEntityCache().findInCacheAsCachedObjects(entitySubClass);
        } while(subclassCachedObjects == null);

        Iterator var9 = subclassCachedObjects.iterator();

        while(var9.hasNext()) {
          CachedEntity subclassCachedObject = (CachedEntity)var9.next();
          EntityImpl cachedSubclassEntity = (EntityImpl) subclassCachedObject.getEntity();
          if (entityMatcher.isRetained((Collection)null, cachedObjects, cachedSubclassEntity, parameter) && !dbSqlSession.isEntityToBeDeleted(cachedSubclassEntity)) {
            result.add(cachedSubclassEntity);
          }
        }
      }
    } else {
      return result;
    }
  }
}
